const connection = require('../app/database')

class RoleService {
  async getUserRole(id) {
    const statement = `
      SELECT role_id roleId FROM users
      WHERE user_id = ?;
    `
    const [res] = await connection.execute(statement,[id])
    return res
  }
  async getRoleList(pagenum = '1',pagesize = '10') {
    const statement = `
      SELECT (SELECT COUNT(*) FROM roles) totalCount,
      JSON_ARRAYAGG(JSON_OBJECT('id',r.role_id,'name',r.role_name,'intro',r.intro)) list
      FROM (SELECT * FROM roles
      LIMIT ?,?) r;
    `
    const offset = (pagenum - 1) * pagesize
    const [result] = await connection.execute(statement, [offset+'', pagesize])
    return result[0]
  }
  async updateRole(id,name,intro) {
    const statement = `
      UPDATE roles SET role_name = ?,intro = ?
      WHERE role_id = ?;
    `
    const [res] = await connection.execute(statement,[name,intro,id])
    return res
  }
  async removeRole(id) {
    const statetment = `
      DELETE FROM roles WHERE role_id = ?;
    `
    const [res] = await connection.execute(statetment,[id])
    return res
  
   
  }
}

module.exports = new RoleService()